package Category

// ToResponse 将Category转换为CategoryResponse
func (c *Category) ToResponse() *CategoryResponse {
	resp := &CategoryResponse{
		ID:        c.ID,
		Name:      c.Name,
		ParentID:  c.ParentID,
		Level:     c.Level,
		Path:      c.Path,
		UserID:    c.UserID,
		CreatedAt: c.CreatedAt,
		UpdatedAt: c.UpdatedAt,
	}

	// 转换父分类
	if c.Parent != nil {
		resp.Parent = c.Parent.ToResponse()
	}

	// 转换子分类
	if len(c.Children) > 0 {
		resp.Children = make([]*CategoryResponse, len(c.Children))
		for i, child := range c.Children {
			resp.Children[i] = child.ToResponse()
		}
	}

	return resp
}

// ToTreeResponse 将Category转换为CategoryTreeResponse
func (c *Category) ToTreeResponse() *CategoryTreeResponse {
	// 使用了深度优先搜索实现
	resp := &CategoryTreeResponse{
		ID:    c.ID,
		Name:  c.Name,
		Level: c.Level,
		Path:  c.Path,
	}

	// 转换子分类
	if len(c.Children) > 0 {
		resp.Children = make([]*CategoryTreeResponse, len(c.Children))
		for i, child := range c.Children {
			resp.Children[i] = child.ToTreeResponse()
		}
	}

	return resp
}
